import user from "../../../class/Utils/User"
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
Page({
    data: {
      xiaoxi_count: 0,
        bangdingtoocletag: false
    },
    onShow: function () {

        var that = this
        var user = wx.getStorageSync('user');
        console.log(user)
        
        if (!user) {
            that.setData({
                shouquan: 1
            })
        } else {
            that.setData({
                shouquan: 0,
                userinfo: user,
                user: user
            })
            http.req("/Mingpian/fafang_panduan", {userid: user.userid}, function (res) {
                that.setData({
                    fafang: res.code,
                    qyid: res.qyid
                })
            })
          var data = {
            userid: user.userid,
            qyid: that.data.qyid
          }
          http.req("/Mingpian/info", data, function (infores) {
            that.setData({
              mpinfo: infores.id,
            })
            console.log(that.data.mpinfo)
          })
            /*
             判断有误 绑定
             */
            if (user.log == '1') {
                http.req('/Api/shengyihao', {uid: user.userid}, function (res) {
                    console.log(res)
                    that.setData({
                        shengyihao: res
                    })
                })
            }
            if (user.log == 1) {
              /*
              获取未读消息数量
              */
              http.req('/Mingpian/xiaoxi', { uid: user.userid }, function (res) {
                console.log(res)
                that.setData({
                  xiaoxi_count: res
                })
              })
            }
        }


    },


    onShareAppMessage: function () {
        var that = this
        return {
            title: "快来添加属于你的企业名片吧",
            path: '/pages/mingpian/bmzw/bmzw?qyid=' + that.data.qyid,
            imageUrl: "https://toocle.y.netsun.com/xcximg/mlogo1.jpg",

            success: function (res) {
            },
        }

    },

    /*
     跳转
     */
    goto: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            console.log(123)
            var url = e.currentTarget.dataset.url
            tips.goto(url)
        })
    },

    goto_mingpian: function (e) {
      var that = this
      /*
      log_user 返回  缓存user
      */
      user.log_user_unid(this, function (res) {
        console.log(123)

        var user = wx.getStorageSync("user")
        http.req("/Mingpian/mp_erweima_panduan", { uid: user.userid }, function (res) {
          console.log(res)
          var url = e.currentTarget.dataset.url
          tips.goto(url)
        })
      })
    },


    /*
     获取个人信息
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res
            })
            http.req('/Api/shengyihao', {uid: res.userid}, function (res) {
                console.log(res)
                that.setData({
                    shengyihao: res
                })
            })
            http.req("/Mingpian/fafang_panduan", {userid: res.userid}, function (res) {
                that.setData({
                    fafang: res.code,
                    qyid: res.qyid
                })
            })
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                wx.setStorageSync('user', res.user)
                tips.ok('登录成功')
                that.setData({
                    userinfo: res.user,
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    /*
     goto_erweima
     */
    goto_erweima: function () {
        var that = this
        user.log_user(that, function () {
            var user = wx.getStorageSync("user")
            http.req("/Mingpian/mp_erweima_panduan", {uid: user.userid}, function (res) {
                console.log(res)
                if (res.code == 200) {
                    /*
                     返回200 说明有二维码
                     */
                    tips.goto('/pages/main/erweima/erweima');
                } else {
                    tips.error(res.msg_val)
                }
            })
        })
    },
    //获取绑定微信的手机号码
    getPhoneNumber: function (e) {
      var that = this
      user.getPhoneNumber(e, that)
    },

  clear_huancun: function () {
    var user = wx.getStorageSync("user")
    user.log = 0
    wx.setStorageSync('user', user)
    this.setData({
      userinfo: user
    })
    tips.ok("操作成功!")
  },

})